package guiFrames;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

import controlador.Controlador;
import entidades.Obrero;
import entidades.Proyecto;
import excepciones.FaltanCompletarCamposException;
import excepciones.FechaNoPuedeSerMayorException;
import excepciones.FormatoFechaInvalidoException;
import excepciones.NoSeEncontraronObrerosException;



@SuppressWarnings("serial")
public class FrameRegAusentismo extends javax.swing.JFrame {

	{
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JButton btnAceptar;
	private JComboBox cmbObreros;
	private JLabel lblNotas;
	private JTextArea txtArea;
	private JCheckBox chkJustificada;
	private JLabel jLabel4;
	private JTextField txtFecha;
	private JLabel jLabel2;
	private JComboBox cmbProyectos;
	private JLabel jLabel1;
	private JButton btnCancelar;
	private List<Obrero> listaObreros;
	private List<Proyecto> listaProyectos;
	private Controlador controlador;
    private Proyecto proyectoSeleccionado;
    
		
	public FrameRegAusentismo(Controlador cont) throws Throwable {
		super();
		controlador = cont;
		this.setTitle("Registrar Ausentismo");
		listaProyectos = controlador.getControladorProyecto().obtenerListaProyectosActivos();
		initGUI();
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		
	}
	
	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				btnAceptar = new JButton();
				btnAceptar.setText("Aceptar");
				btnAceptar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						try {
							
							controlador.getControladorAusencia().altaAusencia(
									Date.valueOf(txtFecha.getText()), 
									chkJustificada.isSelected(), 
									txtArea.getText(), 
									(Proyecto) proyectoSeleccionado, 
									(Obrero) cmbObreros.getSelectedItem()
									);
							limpiarCampos();
						}  catch (IllegalArgumentException e){
							FormatoFechaInvalidoException e1 = new FormatoFechaInvalidoException();
							JOptionPane.showMessageDialog(FrameRegAusentismo.this, e1.getMessage());
						} catch (FechaNoPuedeSerMayorException e){
							JOptionPane.showMessageDialog(FrameRegAusentismo.this, e.getMessage());
						} catch (FaltanCompletarCamposException e) {
							JOptionPane.showMessageDialog(FrameRegAusentismo.this, e.getMessage());
						}catch (Exception e) {
							JOptionPane.showMessageDialog(FrameRegAusentismo.this, e.getMessage());
							limpiarCampos();
						} catch (Throwable e) {
							JOptionPane.showMessageDialog(FrameRegAusentismo.this, e.getMessage());
						}
						
					}
				});
			}
			{
				btnCancelar = new JButton();
				btnCancelar.setText("Cancelar");
				btnCancelar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						dispose();
					}
				});
			}
			{
				jLabel1 = new JLabel();
				jLabel1.setText("1. Seleccionar Proyecto:");
			}
			{
				jLabel4 = new JLabel();
				jLabel4.setText("2. Seleccionar Obrero:");
			}
			{
				cmbObreros = new JComboBox();
			}
			{
				jLabel2 = new JLabel();
				jLabel2.setText("Fecha de ausencia:");
			}
			{
				txtFecha = new JTextField();
				txtFecha.setText("2010-04-25");
			}
			{
				chkJustificada = new JCheckBox();
				chkJustificada.setText("Justificada");
			}
			{
				txtArea = new JTextArea();
				Font font = new Font("Arial Narrow", 0, 14);
				txtArea.setFont(font);
				
			}
			{
				lblNotas = new JLabel();
				lblNotas.setText("Notas:");
			}
			{
				cmbProyectos = new JComboBox();
				cmbProyectos.addItemListener(new ItemListener() {
					public void itemStateChanged(ItemEvent evt) {
						if(cmbProyectos.getSelectedItem() != null){
						try {
							cmbObreros.setEnabled(true);
							btnAceptar.setEnabled(true);
							txtArea.setEnabled(true);
							chkJustificada.setEnabled(true);
							jLabel4.setEnabled(true);
							jLabel2.setEnabled(true);
							txtFecha.setEnabled(true);
							lblNotas.setEnabled(true);
							
							proyectoSeleccionado = (Proyecto) cmbProyectos.getSelectedItem();
							cmbObreros.removeAllItems();
            				listaObreros = new ArrayList<Obrero>();
							listaObreros = controlador.getControladorObrero().obetenerListaObrerosPorProyecto(proyectoSeleccionado);
							cargarComboObrero(listaObreros);
							cmbObreros.setSelectedItem(null);
						
						} catch(NoSeEncontraronObrerosException e){
							cmbObreros.setEnabled(false);
							btnAceptar.setEnabled(false);
							ComboBoxModel cmbExceptionModel = 
								new DefaultComboBoxModel(
										new String[] { e.getMessage() });
							cmbObreros.setModel(cmbExceptionModel);
							txtFecha.setEnabled(false);
							chkJustificada.setEnabled(false);
							txtArea.setEnabled(false);
						} catch (Exception e) {
							JOptionPane.showMessageDialog(FrameRegAusentismo.this, e.getMessage());
						}
						
					}
					}
				});

			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(cmbProyectos, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 18, GroupLayout.PREFERRED_SIZE)
				    .addComponent(jLabel1, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(cmbObreros, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(jLabel4, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 14, GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(txtFecha, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(jLabel2, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addComponent(chkJustificada, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 0, Short.MAX_VALUE)
				.addComponent(lblNotas, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addComponent(txtArea, GroupLayout.PREFERRED_SIZE, 92, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 0, GroupLayout.PREFERRED_SIZE)
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(btnAceptar, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(btnCancelar, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)));
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(thisLayout.createSequentialGroup()
				        .addGroup(thisLayout.createParallelGroup()
				            .addComponent(jLabel1, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 125, GroupLayout.PREFERRED_SIZE)
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(jLabel2, GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE)
				                .addGap(18))
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(jLabel4, GroupLayout.PREFERRED_SIZE, 120, GroupLayout.PREFERRED_SIZE)
				                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED))
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(lblNotas, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE)
				                .addGap(91)))
				        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				        .addGroup(thisLayout.createParallelGroup()
				            .addComponent(txtFecha, GroupLayout.Alignment.LEADING, 0, 252, Short.MAX_VALUE)
				            .addComponent(cmbObreros, GroupLayout.Alignment.LEADING, 0, 252, Short.MAX_VALUE)
				            .addComponent(cmbProyectos, GroupLayout.Alignment.LEADING, 0, 252, Short.MAX_VALUE)
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addGroup(thisLayout.createParallelGroup()
				                    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                        .addGap(72)
				                        .addComponent(btnAceptar, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE))
				                    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                        .addComponent(chkJustificada, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE)
				                        .addGap(62)))
				                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				                .addComponent(btnCancelar, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE)
				                .addGap(0, 0, Short.MAX_VALUE))))
				    .addComponent(txtArea, GroupLayout.Alignment.LEADING, 0, 387, Short.MAX_VALUE))
				.addContainerGap());
			thisLayout.linkSize(SwingConstants.HORIZONTAL, new Component[] {btnCancelar, btnAceptar});
			pack();
			this.setSize(423, 292);
			
			cargarComboProyecto(listaProyectos);
			limpiarCampos();
			
		} catch (Exception e) {
			JOptionPane.showMessageDialog(FrameRegAusentismo.this, e.getMessage());
		}
	}
	
    public void cargarComboProyecto(List<Proyecto> l ){
    	for (Proyecto proyecto : l) {
                    cmbProyectos.addItem(proyecto);
            }
    }
    
	 public void cargarComboObrero(List<Obrero> l ){
     	for (Obrero obrero : l) {
                 cmbObreros.addItem(obrero);
         }
     }
	 
	 public void limpiarCampos(){
		cmbObreros.setSelectedItem(null);
		cmbProyectos.setSelectedItem(null);
		proyectoSeleccionado = new Proyecto();
		txtArea.setText("");
		chkJustificada.setSelected(false);
		cmbObreros.setEnabled(false);
		txtArea.setEnabled(false);
		chkJustificada.setEnabled(false);
		jLabel4.setEnabled(false);
		jLabel2.setEnabled(false);
		txtFecha.setEnabled(false);
		lblNotas.setEnabled(false);
	 }

}
